package com.cn.wanxi.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.common.ImgUpload;
import com.cn.wanxi.entity.ProductEntity;
import com.cn.wanxi.entity.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/backimg/product")
public class ProductController {
    @RequestMapping("/add")
    @ResponseBody
    public void add(ProductEntity productEntity) {
        System.out.println(productEntity);
    }

    @RequestMapping("/pic")
    public Result add(@RequestParam("pic") MultipartFile multipartFile, HttpServletResponse resp) throws IOException {
        String logoName = multipartFile.getOriginalFilename();
        String fileName = UUID.randomUUID() + "-----" + logoName;

        String path = "E:\\wanxi\\step-seven\\canyin07-1224-xadmin\\loadImg/";
        File file = new File(path + fileName);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result("/loadImg/" + fileName, 0);
//        System.out.println(productEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("url", "/loadImg/" + fileName);
//        resp.getWriter().println(jsonObject);
    }
}
